package top.ranzhang.config;



import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import top.ranzhang.interceptor.JwtTokenAdminInterceptor;
import top.ranzhang.interceptor.JwtTokenOtherInterceptor;

import java.text.SimpleDateFormat;
import java.util.List;

@Slf4j
@Configuration
@RequiredArgsConstructor
public class WebMvcConfiguration extends WebMvcConfigurationSupport {



    private final JwtTokenAdminInterceptor jwtTokenAdminInterceptor;

    private final JwtTokenOtherInterceptor jwtTokenOtherInterceptor;

    public void addInterceptors(InterceptorRegistry registry) {
        // 注册自定义拦截器，添加拦截路径和排除拦截路径
        registry.addInterceptor(jwtTokenAdminInterceptor)
                .addPathPatterns("/admin/**")
                .excludePathPatterns("/admin/admin/login")
                .excludePathPatterns("/admin/admin/getCode");

        registry.addInterceptor(jwtTokenOtherInterceptor)
                .addPathPatterns("/register/**")
                .excludePathPatterns("/register/loginRedis")
                .excludePathPatterns("/register/addOther")
                .excludePathPatterns("/register/loginEmail")
                .excludePathPatterns("/register/getEmailCode");

    }




    @Override
    protected void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        // 创建 Jackson 的 ObjectMapper
        ObjectMapper objectMapper = new ObjectMapper();

        // 设置全局日期格式
        objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));

        // 支持 Java 8 时间类型（如 LocalDateTime）
        objectMapper.registerModule(new JavaTimeModule());

        // 添加自定义的消息转换器
        converters.add(new MappingJackson2HttpMessageConverter(objectMapper));

        // 调用父类方法以保留默认的转换器
        super.configureMessageConverters(converters);
    }


}
